
package gui.workspace;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * Text editor for tab in Workspace of system
 * - allows users to write programs
 * - later extensions would include processing text data(coloring,etc)
 * 
 * @author Troy Ferrell
 */

public class WorkspaceEditor extends JPanel implements KeyListener 
{
	public JTextArea textArea;
	private JScrollPane scrollPane;
	
	public WorkspaceEditor()
	{
		this("");
	}
	
	public WorkspaceEditor(String data)
	{
		textArea = new JTextArea();
		textArea.setText(data);
		textArea.setLineWrap(true);
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		scrollPane.setPreferredSize(new Dimension(500, 500));
		this.add(scrollPane);
		
		this.setVisible(true);
	}
	
	/**
	 * select all text in editor
	 */
	public void selectAll()
	{
		textArea.setSelectionStart(0);
		textArea.setSelectionEnd(textArea.getText().length());
	}
	
	/**
	 * return selected text in editor
	 * @return
	 */
	public String getSelectedText()
	{
		return textArea.getSelectedText();
	}
	
	public String getAllText()
	{
		return textArea.getText();
	}
	
	public void insertTextAtCaret(String text)
	{
		int pos = textArea.getCaret().getDot();
		
		String before = textArea.getText().substring(0, pos);
		String after = textArea.getText().substring(pos);
		
		textArea.setText(before + text + after);
		textArea.setCaretPosition(pos + text.length());
	}

	@Override
	// TODO: to use later for processing text information in editor
	public void keyPressed(KeyEvent arg0) {}
	public void keyReleased(KeyEvent arg0) {}
	public void keyTyped(KeyEvent arg0) {}
}
